// pages/coupon/coupon.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        status: "available",
        exchange: false,
        is_cart: "",
        coupon_list_data: [],
        code: "",
        coupon_id: "",
        tips_is_show: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //   从购物车来传1，从个人中心来传2
        var that = this;
        var flag = options.flag;
        if (flag == "order") {
            that.setData({
                is_cart: 1
            })
        } else if (flag == "center") {
            that.setData({
                is_cart: 2
            })
        }
        setTimeout(function () {
            that.available();//可用优惠券
        }, 200)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        that.get_storge();
        setTimeout(function () {
            that.get_coupon_from_storage();//在本地存储里获取优惠券信息选中
        }, 1000)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },//可用
    available: function () {
        var that = this;
        that.setData({
            status: "available",
            select: 1
        })
        var params = {};
        params['select'] = 1;
        wx.getStorage({
            key: 'cartIDs',
            success: function (res) {
                params['cartIDs'] = res.data;
            }
        })
        setTimeout(function () {
            that.coupon_list(params)
        }, 500)
    },//不可用
    useless: function () {
        var that = this;
        that.setData({
            status: "useless",
            select: 2
        })
        var params = {};
        params['select'] = 2;
        wx.getStorage({
            key: 'cartIDs',
            success: function (res) {
                params['cartIDs'] = res.data;
            }
        })
        setTimeout(function () {
            that.coupon_list(params)
        }, 500)
    },//兑换码兑换
    exchanges: function () {
        var that = this;
        that.setData({
            exchange: !that.data.exchange
        })
    },
    coupon_list: function (params) {
        var that = this;
        params['is_cart'] = this.data.is_cart;
        params['phone_number'] = that.data.phone_number;
        that.coupon_list_request(params);
    },//
    exchange_coupon: function () {
        var that = this;
        var code = this.data.code;
        if (code.length != 8) {
            that.showToasts("兑换码无效");
            return;
        }
        var params = {};
        params['phone_number'] = that.data.phone_number;
        params['code'] = code;
        wx.request({
            url: app.config() + 'activitycoupon/getCouponFromCode',
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.status == 0) {
                    var args = {};
                    args['select'] = 1;
                    that.coupon_list(args);
                    that.setData({
                        exchange: false
                    })
                    that.showToasts("兑换成功");
                } else {
                    that.showToasts("兑换码无效");
                }
            }
        })
    },
    inputChange: function (e) {
        var code = e.detail.value;
        this.setData({
            code: code
        })
    },
    showToasts: function (title) {
        wx.showToast({
            title: title,
            image: "../../images/warning.png",
            duration: 2000
        })
    },
    coupon_list_request: function (params) {
        var that = this;
        var is_cart = that.data.is_cart;//从购物车来传1，从个人中心来传2
        var select = that.data.select;//待使用传1，不可用传2
        var re = /^[0-9]+.?[0-9]*$/;//判断是否为数字正则
        wx.request({
            url: app.config() + 'activitycoupon/newCouponList',
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.status == 0) {
                    var type_lang = { "1": "活动发放", "2": "新用户专享", "3": "积分兑换", "4": "朋友分享", "5": "兑换码兑换" };
                    var data = res.data.data;
                    var coupon_data = new Array();
                    for (var key in data) {
                        if (is_cart == 1 && select == 1) {
                            if (key == 'can_use') {
                                for (var k in data[key]) {
                                    coupon_data.push(data[key][k]);
                                }
                            }
                        }
                        else if (is_cart == 2 && select == 1) {
                            if (key == 'can_use') {
                                for (var k in data[key]) {
                                    data[key][k]['used_type'] = "可使用";
                                    coupon_data.push(data[key][k]);
                                }
                            }
                        }
                        else if (is_cart == 1 && select == 2) {
                            if (key == 'can_not_use') {
                                for (var k in data[key]) {
                                    data[key][k]['used_type'] = "不满足最低使用金额";
                                    coupon_data.push(data[key][k]);
                                }
                            }
                        }
                        else if (is_cart == 2 && select == 2) {
                            if (key == 'can_use') {
                                for (var k in data[key]) {
                                    data[key][k]['used_type'] = "已使用";
                                    coupon_data.push(data[key][k]);
                                }
                            } else if (key == 'can_not_use') {
                                for (var k in data[key]) {
                                    data[key][k]['used_type'] = "已过期";
                                    coupon_data.push(data[key][k]);
                                }
                            }
                        }
                    }
                    var len = coupon_data.length;
                    var cur_time = Date.parse(new Date()) / 1000;//当前时间戳
                    for (var i = 0; i < len; i++) {
                        var data = coupon_data[i];
                        var dead_time = data['dead_time'];//过期时间
                        if (dead_time > cur_time) {
                            var last_time = parseInt((parseInt(dead_time) - parseInt(cur_time)) / 86400)+1;
                            data['type'] = type_lang[data['type']];
                            data['selected'] = false;
                            data['last_time'] = last_time;
                        } else {
                            data['type'] = type_lang[data['type']];
                            data['last_time'] = 0;
                        }
                    }
                    if (coupon_data.length) {
                        that.setData({
                            coupon_list_data: coupon_data,
                            tips_is_show: false
                        })
                    } else {
                        that.setData({
                            coupon_list_data: coupon_data,
                            tips_is_show: true
                        })
                    }

                }
            }
        })
    },//从订单选择优惠券支付
    get_coupon: function (e) {
        var that = this;
        var is_cart = that.data.is_cart;
        var data = that.data.coupon_list_data;
        if (is_cart == 1) {
            var coupon_id = e.currentTarget.dataset.id;
            var coupon_amount = e.currentTarget.dataset.amount;
            var index = e.currentTarget.dataset.index;
            data[index]['selected'] = !data[index]['selected'];
            var selected = data[index]['selected'];
            if (selected) {
                that.setData({
                    coupon_id: coupon_id,
                    coupon_amount: coupon_amount,
                    selected_index: index,
                    coupon_list_data: data
                })
            } else {
                that.setData({
                    coupon_id: "",
                    coupon_amount: "",
                    selected_index: "",
                    coupon_list_data: data
                })
            }
            var coupon_obj = {};
            coupon_obj['coupon_id'] = that.data.coupon_id;
            coupon_obj['coupon_amount'] = that.data.coupon_amount;
            coupon_obj['selected_index'] = that.data.selected_index;
            wx.setStorage({
                key: 'coupon_obj',
                data: coupon_obj,
                success: function () {
                 
                }
            })
        }
    },//做选中
    get_coupon_from_storage: function () {
        var that = this;
        var coupon_list_data = that.data.coupon_list_data;
        wx.getStorage({
            key: 'coupon_obj',
            success: function (res) {
                var data = res.data;
                var coupon_id = data.coupon_id;//优惠券id
                var selected_index = data.selected_index;//优惠券id
                coupon_list_data[selected_index]['selected'] = !coupon_list_data[selected_index]['selected'];
                that.setData({
                    coupon_id: coupon_id,
                    selected_index: selected_index,
                    coupon_list_data: coupon_list_data
                })
            }
        })
    },
    get_storge: function () {
        var that = this;
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var member_id = data.id;//会员id
                var phone_number = data.phone_number;//会员电话
                that.setData({
                    member_id: member_id,
                    phone_number: phone_number
                })
            },
        })
    },//确认使用优惠券
    cart_to_use_conpon: function () {
        var that = this;
        wx.navigateBack({
            delta: 1
        })
    }
})
